package com.data.hijack.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * @author husq
 */
public class DateUtils {
    private static Map<String, ThreadLocal<SimpleDateFormat>> sdfCacle = new ConcurrentHashMap<>();

    private DateUtils() {
        throw new RuntimeException("no");
    }

    /**
     * 
     * @param pattern
     * @return
     */
    public static String getLastDayOfMonth(String pattern) {
        LocalDate date = LocalDate.now();
        LocalDate lastDate = date.with(TemporalAdjusters.lastDayOfMonth());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        String lastDayStr = lastDate.format(formatter);

        return lastDayStr;
    }

    /**
     * 
     * @param pattern
     * @return
     */
    public static String getFirstDayOfMonth(String pattern) {
        LocalDate date = LocalDate.now();
        LocalDate lastDate = date.with(TemporalAdjusters.firstDayOfMonth());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        String lastDayStr = lastDate.format(formatter);

        return lastDayStr;
    }

    public static String getDate(String pattern) {
        SimpleDateFormat sdf = getDateFormat(pattern);
        sdf.applyPattern(pattern);
        return sdf.format(new Date());
    }

    public static String getDate(String pattern, Date date) {
        SimpleDateFormat sdf = getDateFormat(pattern);
        sdf.applyPattern(pattern);
        return sdf.format(date);
    }

    /**
     * 
     * @param pattern
     * @param dateStr
     * @return
     */
    public static Date getDate(String pattern, String dateStr) {
        SimpleDateFormat sdf = getDateFormat(pattern);
        if (StringUtils.isBlank(dateStr)) {
            dateStr = getDate(pattern);
        } else {
            sdf.applyPattern(pattern);
        }

        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *
     * @param pattern
     * @return
     */
    private static SimpleDateFormat getDateFormat(String pattern) {
        ThreadLocal<SimpleDateFormat> tl = sdfCacle.get(pattern);
        if (null == tl) {
            tl = new ThreadLocal<SimpleDateFormat>() {
                @Override
                protected SimpleDateFormat initialValue() {
                    return new SimpleDateFormat(pattern);
                }
            };
            sdfCacle.put(pattern, tl);
        }

        return tl.get();
    }

    /**
     *
     * @param localDateTime
     * @param srcPattern
     * @param destPattern
     * @return
     */
    public static String convert(String localDateTime, String srcPattern, String destPattern) {
        LocalDateTime ld = LocalDateTime.parse(localDateTime, DateTimeFormatter.ofPattern(srcPattern));
        return ld.format(DateTimeFormatter.ofPattern(destPattern));
    }
}
